package com.znufe.exam.third.votesdk.impl;

import com.znufe.exam.domain.vote.VoteConfigInfo;
import com.znufe.exam.service.VoteConfigInfoService;
import com.znufe.exam.third.votesdk.JConnectCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ProjectName: 08-uexam
 * @Package: com.znufe.exam.third.votesdkTest
 * @ClassName: JOnConnect
 * @Author: bot
 * @Description:
 * @Date: 2020/6/14 13:57
 * @Version: 1.0
 * @deprecated :连接基站
 */
@Component
public class JOnConnect implements JConnectCallback {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private static JOnConnect jOnConnect;

    @Autowired
    public VoteConfigInfoService voteConfigInfoServiceImpl;
    //通过@PostConstruct实现初始化bean之前进行的操作
    @PostConstruct
    public void init() {
        jOnConnect = this;
        jOnConnect.voteConfigInfoServiceImpl = this.voteConfigInfoServiceImpl;
    }


    @Override
    public int invoke(short BasementID, byte Channel, short SystemMatchCode, short Modal, byte Connected) {
        logger.info("JConnect invoke connected is begin ");
        VoteConfigInfo voteConfigInfo = new VoteConfigInfo();
        voteConfigInfo.setVoteBaseId(String.valueOf(BasementID));
        switch(Connected)
        {
            case 0x01:
                logger.info("JConnect invoke connected is ok ");
                voteConfigInfo.setVoteBaseStatus(String.valueOf(Connected));
                break;
            case 0x00:
                logger.info("JConnect invoke connected is fail ");
                voteConfigInfo.setVoteBaseStatus(String.valueOf(Connected));
                break;
        }

        try {
            int rg = jOnConnect.voteConfigInfoServiceImpl.insertSelective(voteConfigInfo);
            if (rg>0) {
                logger.info("添加答题器基站信息表执行成功："+voteConfigInfo.toString());
            } else {
                logger.error("添加答题器基站信息表执行失败："+voteConfigInfo.toString());
            }
        } catch (Exception e) {
            logger.error("添加答题器基站信息表执行异常："+e.getMessage());
        }

        System.out.println("connected is:" + Connected);
        return Connected;
    }
}
